/*各组件根据打开窗口大小  初始化响应一次*/
baseOnWindowSizeToChange();

/*窗口宽度高度发生变化时，触发各组件响应函数*/
$(window).resize(
    function() {
        baseOnWindowSizeToChange();
    }
);

/*窗口宽度高度发生变化时，各组件应当的响应*/
function baseOnWindowSizeToChange() {
    //获取底部单个选项宽度大小并赋给上拉表单宽度
    var dropdownWidth = $(".bottomSelect").width();
    $(".dropdownSelect").css("width", dropdownWidth);

}

/* 打开网易云播放器 */
let isopen = 0;
function openMyMusic() {
    console.log(isopen);
    if(isopen === 0) {
        $(".netease_Player").css("display", "block");
        isopen = 1;
    } else {
        $(".netease_Player").css("display", "none");
        isopen = 0;
    }
}

/*注册表单验证*/
//input propertychange change即实时监控键盘输入包括粘贴
$("#username").bind("input propertychange change", function() {
    //获取this，即ipwd的val()值，trim函数的作用是去除空格
    const username = $.trim($(this).val());
    if(username == null || username == ""){
        document.getElementById("usernameMsg").innerHTML = "请输入用户名";
        $("#registerBtn").attr("disabled",true);
    }else {
        document.getElementById("usernameMsg").innerHTML = "";
        $("#registerBtn").attr("disabled",false);
    }
})
$("#password").bind("input propertychange change", function() {
    const pwd = $.trim($(this).val());
    const rpwd = $.trim($("#repassword").val());
    //判空
    if(pwd == null || pwd == ""){
        document.getElementById("passwordMsg").innerHTML = "请输入密码";
        $("#registerBtn").attr("disabled",true);
    }else {
        document.getElementById("passwordMsg").innerHTML = "";
        $("#registerBtn").attr("disabled",false);
    }
    //确认密码验证
    if(pwd != "" && rpwd != "" && pwd != rpwd){
        document.getElementById("repasswordMsg").innerHTML = "两次密码不匹配";
        $("#registerBtn").attr("disabled",true);
    }else {
        document.getElementById("repasswordMsg").innerHTML = "";
        $("#registerBtn").attr("disabled",false);
    }
})
$("#repassword").bind("input propertychange change", function() {
    const rpwd = $.trim($(this).val());
    const pwd = $.trim($("#password").val());
    //确认密码验证
    if(pwd != rpwd){
        document.getElementById("repasswordMsg").innerHTML = "两次密码不匹配";
        $("#registerBtn").attr("disabled",true);
    }else {
        document.getElementById("repasswordMsg").innerHTML = "";
        $("#registerBtn").attr("disabled",false);
    }
})
$("#email").bind("input propertychange change", function() {
    const email = $.trim($(this).val());
    //判空
    if(email == null || email == ""){
        document.getElementById("emailMsg").innerHTML = "请输入邮箱";
        $("#registerBtn").attr("disabled",true);
    }else {
        document.getElementById("emailMsg").innerHTML = "";
        $("#registerBtn").attr("disabled",false);
    }
})

// 取消form表单默认提交数据的事件
$('#registerForm').submit(function () {
    return false;
});
//注册对象
function registerUser(username,password,email) {
    this.username = username;
    this.password = password;
    this.email = email;
}
//注册提交按钮
let registerBtn = document.getElementById("registerBtn");
registerBtn.onclick = function (){
    const username = $("#username").val()
    const password = $("#password").val()
    const  email = $("#email").val()
    // 获取用户输入的数据
    const register = new registerUser(username,password,email);
    // ajax发送请求
    $.ajax({
        type:"POST",
        contentType:"application/json",
        data:JSON.stringify(register),
        url :"http://localhost:2082/register",
        success : function(data) {
            let result = JSON.stringify(data);
            result = eval("("+result+")");
            let code = result.code;
            let msg = result.msg;
            //操作成功
            if(code == 200){
                $('#register').modal('hide');
                spop({
                    template: msg,
                    position  : 'top-center',
                    group: "submit-satus",
                    style: "success",
                    autoclose: 2000
                });
            }
            //操作失败
            if(code == 500){
                // $('#register').modal('hide');
                spop({
                    template: msg,
                    position  : 'top-center',
                    group: "submit-satus",
                    style: "fail",
                    autoclose: 2000
                });
            }
        }
    })
}

// 取消form表单默认提交数据的事件
$('#loginForm').submit(function () {
    return false;
});
//登录提交按钮
let loginBtn = document.getElementById("loginBtn");
loginBtn.onclick = function (){
    const username = $("#login-username").val()
    const password = $("#login-password").val()
    // ajax发送请求
    $.ajax({
        type:"POST",
        contentType:"application/json",
        xhrFields: {
            withCredentials: true
        },
        crossDomain: true,
        url :"http://localhost:2082/login?username="+username+"&password="+password,
        success : function(data) {
            let result = JSON.stringify(data);
            result = eval("("+result+")");
            let code = result.code;
            let msg = result.msg;
            //操作成功
            if(code >= 200 && code < 300){
                let token = result.token;
                let user = result.data;

                //隐藏模态框
                $('#login').modal('hide');
                //跳出提示
                spop({
                    template: msg,
                    position  : 'top-center',
                    group: "submit-satus",
                    style: "success",
                    autoclose: 2000
                });

                //先删除用户的相关数据
                window.sessionStorage.removeItem("token");
                window.sessionStorage.removeItem("user");

                //存放登陆用户信息缓存
                window.sessionStorage.setItem("token", token);
                //storage不支持存放对象，所以要先将对象转换为json
                window.sessionStorage.setItem("user",JSON.stringify(user));
                // //取数据
                // window.sessionStorage.getItem("key");
                // //删除指定的key的数据
                // window.sessionStorage.removeItem("key");
                // //删除所有的数据
                // window.sessionStorage.clear();

                //延时跳转
                if(code == 201){
                    window.setTimeout(function(){
                        location.href = "../guanli/administrators.html?username=" + username + "&email=" + eval("("+JSON.stringify(user)+")").email;
                    },2000);
                }else {
                    window.setTimeout(function(){
                        location.href = "welcome-after.html?username=" + username;
                    },2000);
                }

            }
            //操作失败
            if(code == 500){
                $('#login').modal('hide');
                spop({
                    template: msg,
                    position  : 'top-center',
                    group: "submit-satus",
                    style: "fail",
                    autoclose: 2000
                });
            }
        }
    })
}

//提示登陆后才可以使用的功能
let homeBtn = document.getElementById("tucao-home");
let studyBtn = document.getElementById("study");
let videoBtn = document.getElementById("video");
homeBtn.onclick = function (){
    spop({
        template: "请先完成登录操作",
        position  : 'top-center',
        group: "submit-satus",
        style: "failed",
        autoclose: 2000
    });
}
studyBtn.onclick = function (){
    spop({
        template: "请先完成登录操作",
        position  : 'top-center',
        group: "submit-satus",
        style: "failed",
        autoclose: 2000
    });
}
videoBtn.onclick = function (){
    spop({
        template: "请先完成登录操作",
        position  : 'top-center',
        group: "submit-satus",
        style: "failed",
        autoclose: 2000
    });

}